//
//  MainViewController.swift
//  我的微博
//
//  Created by WUSANZHONG on 15/9/6.
//  Copyright © 2015年 WUSANZHONG. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
    }
    
    //在视图显示之前添加撰写按钮
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        setupComposeButton()
    }
    
    //设置撰写按钮位置
    private func setupComposeButton(){
        
        let width = tabBar.bounds.width / CGFloat(viewControllers!.count)
        
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: tabBar.bounds.height)
    }
    
    //添加板块控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeTableViewController(), title: "首页", name: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", name: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", name: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", name: "tabbar_profile")

    }

    private func addChildViewController(VC:UIViewController, title:String, name:String) {
        
        tabBar.tintColor = UIColor.orangeColor()
        
        let HomeVC = VC
        
        HomeVC.title = title
        HomeVC.tabBarItem.image = UIImage(named: name)

        let nav = UINavigationController(rootViewController: HomeVC)
        
        addChildViewController(nav)
    }
    
    //撰写按钮
    lazy private var composeButton: UIButton = {
        
        let button = UIButton()
//        button.backgroundColor = UIColor.yellowColor()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)

            
        
        self.tabBar.addSubview(button)
        
        return button
        
    }()

}
